package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.alibaba.druid.sql.visitor.functions.If;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private IStatementService statementService;
    @Autowired
    private IAppointmentService appointmentService;


    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<>(statementItemMapper.selectForList(qo));
    }


    @Override
    public void save(StatementItem statementItem) {
        statementItemMapper.insert(statementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(StatementItem statementItem) {
        statementItemMapper.updateByPrimaryKey(statementItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

    @Override
    @Transactional
    public void saveItems(List<StatementItem> items) {
        //获取到最后一条记录,额外携带一些表单信息
        StatementItem tempItem = items.remove(items.size() - 1);
        //获取优惠价格和结算单ID
        BigDecimal discountAmount = tempItem.getItemPrice();
        Statement statement = statementService.get(tempItem.getStatementId());
        //合理性校验
        //如果结算单数据的状态为已支付,则 无法保存明细操作
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            throw new BusinessException("状态为已支付,无法保存明细操作");
        }
        //删除之前保存的明细项数据
        statementItemMapper.deleteByStatementId(statement.getId());

        //总金额
        BigDecimal totalAmount = new BigDecimal(0);
        //总数量
        BigDecimal totalQuantity = new BigDecimal(0);

        if (items.size()>0) {
            //说明有明细项
            for (StatementItem item : items) {
                //保存明细项数据到数据库中
                statementItemMapper.insert(item);
                //计算总金额==> 明细项单价*数量
                totalAmount = totalAmount.add(item.getItemPrice().multiply(item.getItemQuantity()));
                //计算总数量
                totalQuantity=totalQuantity.add(item.getItemQuantity());
            }
        }

        //合理性校验
        //如果优惠金额比总金额都高,属于非法逻辑
        if (totalAmount.compareTo(discountAmount)<0) {
            throw new BusinessException("非法操作,优惠金额不能大于总金额");
        }

        //更新数据库明细项数据
        statementService.updateAmount(statement.getId(),totalAmount,totalQuantity,discountAmount);

    }


    @Override
    public void payStatement(Long statementId) {
        Statement statement = statementService.get(statementId);
        //合理性判断
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            throw new BusinessException("结算单已经支付,请不要重复支付");
        }
        //获取收款人的Id
        Long userId = ShiroUtils.getUserId();
        //更新结算单的状态
        statementService.payStatement(statementId,userId,Statement.STATUS_PAID);
        if (statement.getAppointmentId()!=null) {
            //说明预约单的id是由结算单生成的
            //修改预约单的状态
            appointmentService.changeStatus(statement.getAppointmentId(), Appointment.STATUS_SETTLE);
        }

    }

    @Override
    public void deleteByStatementId(Long id) {
        statementItemMapper.deleteByStatementId(id);
    }
}

